package writer;

import java.util.HashMap;

import javax.jms.JMSException;

import news.News;
import news.NewsEvent;

public class Journalist {

	private HashMap<String, WriterTopic> writers ;
	private String name ;
	
	public Journalist(String name){
		this.name = name ;
		writers = new HashMap<String, WriterTopic>();
		
	}
	
	public void addTopic(String topic){
		
		if( writers.containsKey(topic) ){
			System.out.println("Journalist: "+ name + " already publishes on topic: "+ topic);
			return ;
		}
			
		try {
			WriterTopic wr = new WriterTopic(topic, name, "psw"+name);
			
			writers.put(topic, wr);
			new Thread(wr).start();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void publishNews(String topic, News news, String event){
		
		try {
			WriterTopic wr = writers.get(topic) ;
			if( wr != null  ){
				wr.writeMessage(news, event);
			} else {
				System.err.println("Journalist "+ name +" is not allowed to publish on topic: "+ topic);
			}
				
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
}
